<?php
class UserController extends IndexController
{
    public function init()
    {
        $this->TestModel     = new TestModel();
        $this->UserModel     = new UserModel();
        $this->GroupModel    = new GroupModel();
        $this->UserLogsModel = new LogsModel();
        $this->Session       = Yaf_Registry::get("session");
        $this->utl           = Yaf_Registry::get("utl");	
    } 

    public function loginAction()
    {       

        if ($this->getRequest()->isPost())
        {
            date_default_timezone_set('PRC');
            $arr     = array();
            $nowDate = time();
            for($i = 0; $i<7; $i++){
                $getTime = $nowDate-3600*24*$i;
                $arr[$i] = date("Y-m-d ",$getTime);
            }
            $username = $this->getRequest()->getPost('username');
            $password = $this->getRequest()->getPost('password');
            $allinfo  = $this->UserModel->userLogin($username,md5($password));
            header('Content-type:application/json;charset:utf8');
            if (empty($allinfo))
            {
                exit('{"status":false,"info":"登陆失败, 用户不存在！"}');
                return false;
            }
            foreach ($allinfo AS $k => $v)
            {
                $local_ip = $_SERVER['REMOTE_ADDR'];
                $allinfo[$k]['ip'] = $local_ip;
            }    
            $this->Session->set('user_info', $allinfo);
            $userlogs_login = $this->UserLogsModel->addLogin($allinfo);
            exit('{"status":true,"info":"登录成功"}');
        }
        return true;
    }
    
    public function listAction()
    {
		parent::init();
		$user              = $this->Session->user_info;
        
        $allGroup_info     = $this->GroupModel->selectGroupInfo();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = offset*($page-1);
        $allUser_info      = $this->UserModel->selectUser($startLimit,$offset);
        $allCounts         = $this->UserModel->getAllCount();
        $allpage           = $this->utl::page('/User/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_list         = array();
        foreach ($allUser_info AS $k => $v)
        {
            $user_list[$k]                 = $v;
            $user_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $user_list[$k]['rows']         = $allCounts[0]['rows'];
            $user_list[$k]['every_page']   = $offset;
            foreach ($allGroup_info  AS $ks => $vs)
            {
                if ($v['group_id'] === $vs['group_id'])
                {
                    $user_list[$k]['group_name'] = $vs['name'];break;
                }
            }
        }    
           
        $this->getView()->assign("userList",$user_list);
        $this->getView()->assign("groupList",$allGroup_info);
        $this->getView()->assign("allpage",$allpage);
    }

    public function pageAction()
    {
        $allGroup_info     = $this->GroupModel->selectGroupInfo();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUser_info      = $this->UserModel->selectUser($startLimit,$offset);
        $allCounts         = $this->UserModel->getAllCount();
        $allpage           = $this->utl::page('/User/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $user_list         = array();
        foreach ($allUser_info AS $k => $v)
        {
            $user_list[$k] = $v;
            $user_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            foreach ($allGroup_info  AS $ks => $vs)
            {
                if ($v['group_id'] === $vs['group_id'])
                {
                    $user_list[$k]['group_name'] = $vs['name'];break;
                }
            }
        }
        $this->getView()->assign("userList",$user_list);
        $this->getView()->assign("groupList",$allGroup_info);
        exit(json_encode($user_list, true));
    }

    public function currentPageAction()
    {
		
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allUser_info      = $this->UserModel->selectUser($startLimit,$offset);
        $allCounts         = $this->UserModel->getAllCount();
        $allpage           = $this->utl::page('/User/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $this->getView()->assign("allpage",$allpage);
        exit($allpage);
        return true;
    }

    public function personInfoAction()
    {
		parent::init();
        $personInfo = $this->Session->user_info;
        if ($this->getRequest()->isPost())
        {
            
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            foreach ($personInfo as $key => $value) {
               if(md5($post['passwordOld']) != $value['password']){
                    exit('{"status":false,"info":"原密码输入不正确！"}');
                    return;
               }

            }
            $rs    = $this->UserModel->updatePwd($post);
            if( false  === $rs){
                exit('{"status":false,"info":"修改个人信息失败！"}');
                return;
            }
            
            exit('{"status":true,"info":"修改个人信息成功！"}');

        }
        $this->getView()->assign("person",$personInfo);
    }

    public function loginoutAction() 
    {
        if($this->Session->del('user_info'))
        {
            $this->redirect('/User/login');
        }
    }

    public function infoAction()
    {
		parent::init();
        $user_info = $this->Session->user_info;
        $this->getView()->assign("userinfo",$user_info);
    }


    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('User', 'list');
            return;
        }
        $rs          = $this->UserModel->del($id);
        $session_del = array();
        $user_info   = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id']    = $v['user_id'];
            $session_del[$k]['table_name'] = "user";
            $session_del[$k]['action']     = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('User', 'list');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            if (empty($post['name']))
            {
                exit('{"status":false,"info":"用户名不能为空！"}');
                return;
            }
            if ($this->UserModel->isUserNameExistById($post['name'], $id))
            {
                exit(json_encode(array('status' => false, 'info' => '用户已存在！')));
                return;
            }
            $rs           = $this->UserModel->edit($post, $id);
            $session_edit = array();
            $user_info    = $this->Session->user_info;
            foreach ($user_info as $k => $v) {
             
                $session_edit[$k]['user_id']    = $v['user_id'];
                $session_edit[$k]['table_name'] = "user";
                $session_edit[$k]['action']     = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs)
            {
                exit('{"status":false,"info":"编辑失败！"}');
                return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function addAction()
    {
		
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            if (empty($post['name']))
            {
                exit('{"status":false,"info":"用户名不能为空！"}');
                return;
            }
            if ($this->UserModel->isUserNameExist($post['name']))
            {
                exit('{"status":false,"info":"用户已存在！"}');
                return;
            }
            $rs          = $this->UserModel->add($post);
            $session_add = array();
            $user_info   = $this->Session->user_info;
            foreach ($user_info as $k => $v) {
                $session_add[$k]['user_id']    = $v['user_id'];
                $session_add[$k]['table_name'] = "user";
                $session_add[$k]['action']     = "add";
            }
            $this->UserLogsModel->addUser($session_add);
            if (false === $rs)
            {
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');
        }
    }
}
?>